﻿using System;
using System.IO;
using System.Xml;
using Infrastructure.Serialization;

namespace PasswordControl.UI.WPF.Data
{
    class UserData
    {
        #region static

        private static readonly string UserDataFile = Path.Combine(Configuration.AppDataDir, "UserData.xml");

        private static ISerializer s_serializer = new Serializer();

        private static UserData s_instnace;

        public static UserData Instance
        {
            get { return s_instnace; }
        }

        static UserData()
        {
            if (File.Exists(UserDataFile))
            {
                var xDoc = new XmlDocument();
                xDoc.Load(UserDataFile);
                s_instnace = s_serializer.Deserialize<UserData>(xDoc.SelectSingleNode("*"));
            }
            else
            {
                s_instnace = new UserData();
            }
        }

        #endregion

        #region instnace

        private string m_lastComponentPath;

        [StoredValue]
        public string LastComponentPath 
        {
            get { return m_lastComponentPath; }
            set { m_lastComponentPath = value; StoreData(); }
        }

        private void StoreData()
        {
            var xDoc = new XmlDocument();
            s_serializer.Serialize(this, xDoc);
            xDoc.Save(UserDataFile);
        }

        #endregion
    }
}
